package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.LeaveMsg;
import com.jr9z.jrjz.service.LeaveMsgService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author:Mrzhang
 * @Description:留言管理controller
 * @Date:2019.03.31 14:24
 **/

@Controller
@RequestMapping("/leaveMsg")
public class LeaveMsgController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LeaveMsgService leaveMsgService;


    /**
     * 跳转到留言管理
     * @return
     */
    @RequestMapping("/leaveMsgManage")
    public String  leaveMsgManage(){
        logger.info("跳转到留言管理界面");
        return  "/leavemessage/leaveMsgManage";
    }

    /**
     * 根据留言id查询留言
     *
     * @param msgId
     * @return
     */
    @RequestMapping(value = "/admin/getMsgById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getMsgById(Integer msgId) {

        LeaveMsg leaveMsg;

        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            leaveMsg = leaveMsgService.selectByPrimaryKey(msgId);

            if (leaveMsg == null) {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            } else {
                custom.data(leaveMsg).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * 根据id删除留言
     *
     * @param msgId 留言id
     * @return
     */
    @RequestMapping(value = "/admin/deleteMsg", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteMsgById(Integer msgId) {

        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = leaveMsgService.deleteByPrimaryKey(msgId);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取留言列表（分页）
     *
     * @param currPage 当前页
     * @param pageSize 每页数量
     * @return
     */
    @RequestMapping(value = "/admin/getMsgList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getMsgList(@RequestParam Integer currPage,
                                   @RequestParam Integer pageSize) {

        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {

            List<LeaveMsg> msgList = leaveMsgService.getMSgList();

            Long count = leaveMsgService.getMsgCount();

            if (count != 0 && msgList != null) {
                custom.data(msgList).currPage(currPage).
                        pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();

    }


    /**
     * 添加留言
     *
     * @param leaveMsg json格式的留言实体
     * @return
     */
    @RequestMapping(value = "/addMsg", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addLeaveMsg(@RequestBody LeaveMsg leaveMsg) {

        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = leaveMsgService.addLeaveMsg(leaveMsg);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }
}
